from settings import web_type
from util.DataQuery.DbTools import DbTools
from util.DataQuery.hzflowOperation import hzflowOperation
from util.g import g
from util.inform.sendNote import AliyunSMS
from util.selenium.webdriver.common.by import By

from base.BaseMethod import BM



class ToLeftThree():


    top_frame_loc = (By.CSS_SELECTOR, 'iframe')

    left_frame_loc = (By.CSS_SELECTOR, 'frame#leftFrame')

    title_h_loc = (By.CSS_SELECTOR, "h2")

    accept_loc = (By.CSS_SELECTOR, ".btnBox .btn.ok")

    # left_three_btn_loc = (By.CSS_SELECTOR, 'ul#transmitId li:nth-child(3) a')
    left_three_btn_loc = (By.XPATH, "//ul[@id='transmitId']//a[contains(text(),'高企认定申报')]")

    def __init__(self, driver, status=None):
        self.driver = driver
        self.status = status

    def to_left_three(self):

        BM.close_current_page(driver=self.driver)
        page_list = self.driver.window_handles
        if len(page_list) > 1:
            self.driver.switch_to.window(page_list[-1])

        if BM.isElementExist(element=self.title_h_loc, driver=self.driver):
            tip = self.driver.find_element(*self.title_h_loc).text
            if u"未实名认证" in tip:
                status = self.status
                map = {"ul_status": -1, "c_idstatus": 1}
                DbTools.update_entrestatus_fields(map, co_id=g.co_id, year=g.year)
                hzflowOperation.update_host_process(obj=hzflowOperation.get_log(g.co_id, g.year), mark_info="")
                AliyunSMS().send_single(
                    phone=status.get("phone"), template="SMS_193244862",
                    params={"resinfo": "{}, 企业国网账号未实名认证".format(status.get("entname"))},
                    fb_time=status.get("fb_time"), res_code=web_type)
                self.driver.quit()
            return True

        if BM.isElementExist(element=self.accept_loc, driver=self.driver):
            BM.click(driver=self.driver, ele_loc=self.accept_loc)

        # # # # # 此时刷新页面，使页面能更快的加载出来。
        # self.driver.refresh()

        # print(self.__class__.__name__)
        BM.wait(driver=self.driver, ele_loc=self.top_frame_loc, time=20)

        BM.cut_over_frame(self.driver, self.top_frame_loc)

        self.driver.switch_to.parent_frame()
        # BM.cut_over_frame(self.driver, self.left_frame_loc)

        BM.click(driver=self.driver, ele_loc=self.left_three_btn_loc)

        return False
